//-----------------------------------------------------------------------
// <copyright file="OfficeDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class OfficeDAOBase
    {
        #region Common methods
        public virtual Office CreateOfficeFromReader(IDataReader reader)
        {
            Office item = new Office();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("OfficeID"))) item.OfficeID = (int)reader["OfficeID"];
                if (!reader.IsDBNull(reader.GetOrdinal("OfficeName"))) item.OfficeName = (string)reader["OfficeName"];
                if (!reader.IsDBNull(reader.GetOrdinal("OfficeCode"))) item.OfficeCode = (string)reader["OfficeCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("NotifyEmailEnable"))) item.NotifyEmailEnable = (bool)reader["NotifyEmailEnable"];
                if (!reader.IsDBNull(reader.GetOrdinal("SortOrder"))) item.SortOrder = (int)reader["SortOrder"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateOfficeFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateOffice methods
            
        public virtual void CreateOffice(Office office)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spOfficesInsert");
                
                dbCommandWrapper.AddInParameter("@OfficeName", DbType.String, office.OfficeName);
                dbCommandWrapper.AddInParameter("@OfficeCode", DbType.AnsiString, office.OfficeCode);
                dbCommandWrapper.AddInParameter("@NotifyEmailEnable", DbType.Boolean, office.NotifyEmailEnable);
                dbCommandWrapper.AddInParameter("@SortOrder", DbType.Int32, office.SortOrder);
                dbCommandWrapper.AddOutParameter("@OfficeID", DbType.Int32, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
                office.OfficeID = (int)dbCommandWrapper.GetParameterValue("@OfficeID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateOfficeException, ex);
            }
        }

        #endregion

        #region UpdateOffice methods
        
        public virtual void UpdateOffice(Office office)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spOfficesUpdate");            
                
                dbCommandWrapper.AddInParameter("@OfficeID", DbType.Int32, office.OfficeID);
                dbCommandWrapper.AddInParameter("@OfficeName", DbType.String, office.OfficeName);
                dbCommandWrapper.AddInParameter("@OfficeCode", DbType.AnsiString, office.OfficeCode);
                dbCommandWrapper.AddInParameter("@NotifyEmailEnable", DbType.Boolean, office.NotifyEmailEnable);
                dbCommandWrapper.AddInParameter("@SortOrder", DbType.Int32, office.SortOrder);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateOfficeException, ex);
            }
        }
        
        #endregion

        #region DeleteOffice methods
        public virtual void DeleteOffice(int officeID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spOfficesDelete");
                
                dbCommandWrapper.AddInParameter("@OfficeID", DbType.Int32, officeID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteOfficeException, ex);
            }
        }

        #endregion

        #region GetOffice methods
        
        public virtual Office GetOffice(int officeID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spOfficesGet");
                
                dbCommandWrapper.AddInParameter("@OfficeID", DbType.Int32, officeID);
                
                Office office =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        office = CreateOfficeFromReader(reader);
                        reader.Close();
                    }
                }
                return office;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetOfficeException, ex);
            }
        }
        
        #endregion

        #region GetOffices methods
        public virtual OfficeCollection GetOffices(OfficeColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spOfficesGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                OfficeCollection officeCollection = new OfficeCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        Office office = CreateOfficeFromReader(reader);
                        officeCollection.Add(office);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return officeCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetOfficesException, ex);
            }
        }
        
        public virtual OfficeCollection GetOffices(OfficeColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetOffices(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}